class IniData:
    def __init__(self):
        self.filename = ""
        self.X = 0.0
        self.Y = 0.0
        self.FaultX = 0.0
        self.FaultY = 0.0
        self.PixelSizeX = 0.0
        self.PixelSizeY = 0.0
        self.ImageSizeX = 0.0
        self.ImageSizeY = 0.0
        self.Col = 0.0
        self.Row = 0.0
        self.Type = 0.0
        self.Index = 0.0
        self.IsTableCoordinate = 0.0
        self.IsColor = 0.0
        self.IsGreyRevisit = 0.0
        self.IsBsi = 0.0
        self.IsClip = 0.0
        self.IsClip2 = 0.0

    def read_ini_file(self, file_path,ini_data_list):

        try:
            with open(file_path, 'r') as infile:
                for line in infile:
                    line = line.strip()
                    if line and line[0] == '[':
                        # 如果是新图片的标志，创建对应的IniData实例并将其加入到列表中
                        data = IniData()
                        data.filename = line[1:-1]  # 去掉方括号
                        ini_data_list.append(data)
                    else:
                        # 解析键值对
                        pos = line.find('=')
                        if pos != -1:
                            key = line[:pos]
                            value = line[pos + 1:]
                            try:
                                dvalue = float(value)
                                # 如果能够解析成float类型，就保存到对应的IniData实例中
                                if key == "X":
                                    ini_data_list[-1].X = dvalue
                                elif key == "Y":
                                    ini_data_list[-1].Y = dvalue
                                elif key == "FaultX":
                                    ini_data_list[-1].FaultX = dvalue
                                elif key == "FaultY":
                                    ini_data_list[-1].FaultY = dvalue
                                elif key == "PixelSizeX":
                                    ini_data_list[-1].PixelSizeX = dvalue
                                elif key == "PixelSizeY":
                                    ini_data_list[-1].PixelSizeY = dvalue
                                elif key == "ImageSizeX":
                                    ini_data_list[-1].ImageSizeX = dvalue
                                elif key == "ImageSizeY":
                                    ini_data_list[-1].ImageSizeY = dvalue
                                elif key == "Col":
                                    ini_data_list[-1].Col = dvalue
                                elif key == "Row":
                                    ini_data_list[-1].Row = dvalue
                                elif key == "Type":
                                    ini_data_list[-1].Type = dvalue
                                elif key == "Index":
                                    ini_data_list[-1].Index = dvalue
                                elif key == "IsTableCoordinate":
                                    ini_data_list[-1].IsTableCoordinate = dvalue
                                elif key == "IsColor":
                                    ini_data_list[-1].IsColor = dvalue
                                elif key == "IsGreyRevisit":
                                    ini_data_list[-1].IsGreyRevisit = dvalue
                                elif key == "IsBsi":
                                    ini_data_list[-1].IsBsi = dvalue
                                elif key == "IsClip":
                                    ini_data_list[-1].IsClip = dvalue
                                elif key == "IsClip2":
                                    ini_data_list[-1].IsClip2 = dvalue
                                else:
                                    print("Unknown key:", key)
                            except ValueError:
                                print("Invalid value for key", key)
                        else:
                            print("Invalid line:", line)

        except IOError as e:
            print(e)
            print("Failed to open file:", file_path)

        return ini_data_list


    def write_ini_file(self, file_path, ini_data_list):
        try:
            with open(file_path, 'w') as outfile:
                for data in ini_data_list:
                    outfile.write("[" + data.filename + "]\n")
                    outfile.write("X=" + format(data.X, '.10f') + "\n")
                    outfile.write("Y=" + str(data.Y) + "\n")
                    outfile.write("FaultX=" + str(data.FaultX) + "\n")
                    outfile.write("FaultY=" + str(data.FaultY) + "\n")
                    outfile.write("PixelSizeX=" + str(data.PixelSizeX) + "\n")
                    outfile.write("PixelSizeY=" + str(data.PixelSizeY) + "\n")
                    outfile.write("ImageSizeX=" + str(data.ImageSizeX) + "\n")
                    outfile.write("ImageSizeY=" + str(data.ImageSizeY) + "\n")
                    outfile.write("Col=" + str(data.Col) + "\n")
                    outfile.write("Row=" + str(data.Row) + "\n")
                    outfile.write("Type=" + str(data.Type) + "\n")
                    outfile.write("Index=" + str(data.Index) + "\n")
                    outfile.write("IsTableCoordinate=" + str(data.IsTableCoordinate) + "\n")
                    outfile.write("IsColor=" + str(data.IsColor) + "\n")
                    outfile.write("IsGreyRevisit=" + str(data.IsGreyRevisit) + "\n")
                    outfile.write("IsBsi=" + str(data.IsBsi) + "\n")
                    outfile.write("IsClip=" + str(data.IsClip) + "\n")
                    outfile.write("IsClip2=" + str(data.IsClip2) + "\n")
                    # outfile.write("\n")

        except IOError as e:
            print("Failed to create file:", file_path)
